/*
 * Copyright (c) 2025 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.helidon.service.jndi;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import io.helidon.testing.junit5.Testing;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;

@Testing.Test
public class JndiTest {

    static {
        // make sure the JNDI factory is specified
        NamingFactory.register();
    }

    @Test
    public void testRootContext() throws NamingException {
        InitialContext ctx = new InitialContext();
        assertThat(ctx.createSubcontext(""), instanceOf(RootContext.class));
    }

    @Test
    public void testChildContext() throws NamingException {
        InitialContext ctx = new InitialContext();
        Context subcontext = ctx.createSubcontext("my");
        assertThat(subcontext, instanceOf(ChildContext.class));
        Object bound = subcontext.lookup("jndi/Name");

        assertThat(bound, instanceOf(JndiBoundService.class));
    }
}
